package com.ht.util;
import com.ht.config.DBconfig;
import java.sql.Connection;
import java.sql.DriverManager;


public class DBHelper {

    private  static Connection conn = null;

    static {
        try {
            //加载数据库驱动
            Class.forName(DBconfig.DRIVER);
            conn  = DriverManager.getConnection(DBconfig.URL,DBconfig.USERNAME,DBconfig.PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * @return
     */
    public  static Connection getConnection(){

        if (conn==null){
            try {
                Class.forName(DBconfig.DRIVER);
                conn  = DriverManager.getConnection(DBconfig.URL,DBconfig.USERNAME,DBconfig.PASSWORD);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return conn;
    }
}
